(function() {

	var skill = {
		save: function() {
			db.update(db_table_name, this._id, this);
		},
		destroy: function() {
			db.remove(db_table_name, this._id);
		},
		//非安全，更改数量
		_updateNum: function(num) {
			num = +num;
			num || (num = 1);
			if (num < 1) {
				num = 1;
			}
			cart_goods_info.num = num;
			this.save();
		},
		//非安全，增加抵用类BUFF
		_addBuff: function(buff_id) {
			//TODO：对BUFF的类型进行判断，只能用抵用类BUFF
			var buffs = this.buffs || (this.buffs = []);
			buffs.push(buff_id);
			this.save();
		},
		getBuffs: function() {
			var buffs = (this.buffs || []).slice();
			var _new_buffs = [];
			var result = [];
			buffs.forEach(function(buff_id) {
				var buff = BuffCon.get(buff_id);
				if (buff && buff.isUseAble()) {
					_new_buffs.push(buff_id);
					result.push(buff);
				}
			});
			//BUFF列表中有过期的或者已经失效的，更新BUFF列表
			if (_new_buffs.length !== buffs.length) {
				this.buffs = _new_buffs;
				this.save();
			}
			return result;
		},
		getBuffsDetail: function() {
			var buffs = this.getBuffs();
			return buffs.map(function(buff_id) {
				BuffCon.get(buff_id);
			});
		},
		//非安全，转化为订单ach
		_toAch: function(info, is_for_cash_com) {
			var user_id = info.user_id;
			var user_address_id = info.user_address_id;

			var recommender_id = info.recommender_id;
			var guide_id = info.guide_id;
			var ach = new AchCon({
				//类，构造器名字
				type: "cart_goods",
				//缓存商品信息，力保届时可以还原页面访问的情形
				goods_info: this.goods_info,
				//支付的基础额度，不包括任何额外数据
				user_id: user_id,
				bus_id: this.bus_id,
				recommender_id: recommender_id,
				guide_id: guide_id,
				user_address_id: user_address_id,
				//干扰它的BUFF详细记录，非商品BUFF（因为商品的BUFF都是独立的），而是用户自己添加的
				buffs: [],
				state: AchCon.STATE.未付款,
				pay_method: info.pay_method
			}, is_for_cash_com);
			return ach._id;
		},
		setGoodsInfo: function(goods_info) {
			//对购物车的的商品信息进行格式化处理并校验数据的合理性
			goods_info.forEach(function(goods_id_and_num) {
				if (!GoodsCon.get(goods_id_and_num.goods_id)) {
					throwCusError("找不到指定商品");
				}
				goods_id_and_num.num = (~~goods_id_and_num.num) || 1;
			});

			//[商品ID以及购买数量]{goods_id:...,num:num}
			this.goods_info = goods_info;
			this.save();
		},
		//非安全，从购物车中删除商品
		_removeGoods: function(goods_id) {
			var new_goods_info = this.goods_info.filter(function(goods_id_and_num) {
				console.log(goods_id_and_num.goods_id, goods_id);
				return goods_id_and_num.goods_id != goods_id
			});
			if (new_goods_info.length !== this.goods_info.length) {
				this.goods_info = new_goods_info;
				this.save();
			}
		},
		_updateGoodsNum: function(goods_id, num) {
			num = +num || 0;
			if (num < 0) {
				throwCusError("购买的商品数量有误");
			}
			var result = this.goods_info.every(function(goods_id_and_num) {
				if (goods_id_and_num.goods_id == goods_id) {
					goods_id_and_num.num = num;
				} else {
					return true;
				}
			});
			if (!result) {
				this.save();
			}
		},
	}
	module.exports = skill;

	var db = require("../db/index");
	var UserCon = require("./_con");
	var BusCon = require("../bus/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
	var PayAchCon = require("../pay_ach/_con");
	var CommentCon = require("../comment/_con");
	var EvaluationCon = require("../eval/_con");
	var ClassifyCon = require("./_con");
	var BuffCon = require("../buff/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var $Array = require("../_include/$.Array");
	var $Object = require("../_include/$.Object");
	var helper = require("../_include/helper");
	var db_table_name = config.dbname.userCartGoods;
	var db_id_prefix = config.dbIdPrefix.userCartGoods;

}());